/*
 * PACKAGE NAME: com.jodinfo.outmirror.api.tabledefine.controller
 * FILE    NAME: TableDefineDaoImpl.java
 * COPYRIGHT: Copyright(c) © 2016 heroj Company Ltd. All Rights Reserved
 */ 
package com.jodinfo.outmirror.api.tabledefine.controller;

import java.math.BigInteger;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.hero.common.web.context.UserContext;
import com.hero.core.constant.SysConstant;
import com.hero.core.entity.ResponseEntity;
import com.jodinfo.outmirror.api.controlwidget.entity.ControlWidgetEntity;
import com.jodinfo.outmirror.api.controlwidget.service.ControlWidgetBizService;
import com.jodinfo.outmirror.api.purbarcode.entity.PurBarcodeEntity;
import com.jodinfo.outmirror.api.tabledefine.entity.SubTableEntity;
import com.jodinfo.outmirror.api.tabledefine.entity.TableDefineEntity;
import com.jodinfo.outmirror.api.tabledefine.service.TableDefineBizService;



@Controller
@RequestMapping(value = "/TableDefineAdmin")
public class TableDefineController  {
	
@Resource(name="tableDefineBizService")
private TableDefineBizService tableDefineBizService;
@Resource(name="controlWidgetBizService")
private ControlWidgetBizService controlWidgetBizService;
/**
*  list数据源
* 
* */
@RequestMapping(value = "/List")
@ResponseBody
public String TableDefineList(TableDefineEntity tableDefineEntity) {
	if(tableDefineEntity==null){
		tableDefineEntity = new TableDefineEntity();
		tableDefineEntity.setPageIndex(SysConstant.DEFAULT_PAGEINDEX);
		tableDefineEntity.setPageSize(SysConstant.DEFAULT_PAGESIZE);
	}
	tableDefineEntity.setCurrentUser(UserContext.getUser());
	String str = tableDefineBizService.queryListForPageTableDefine(tableDefineEntity);
	return str;
}
/**
* TableDefineEntity List页面
* 
* */
@RequestMapping(value = "/ListPage")
public ModelAndView tableDefineListPage(TableDefineEntity tableDefineEntity,HttpServletRequest request) {
	
	return new ModelAndView(returnPath("tableDefineList"));
}

/**
* 表单设计页面
* 
* */
@RequestMapping(value = "/page")
public ModelAndView meterialPriceListPage(TableDefineEntity tableDefineEntity) {
	ControlWidgetEntity controlWidgetEntity = new ControlWidgetEntity();
	if(!checkTableDefineEntity(tableDefineEntity)) {
		tableDefineEntity = new TableDefineEntity();
	}else{
		 tableDefineEntity = tableDefineBizService.queryEntityByTableDefine(tableDefineEntity);
		 if(tableDefineEntity==null){
			 tableDefineEntity = new TableDefineEntity();
		 }
	}
	controlWidgetEntity.setIsInstantiation(SysConstant.SUCCESS);
	return new ModelAndView(returnPath("formDesigner")).addObject("controlWidgetList", controlWidgetBizService.queryAllControlList(controlWidgetEntity)).addObject("dto", tableDefineEntity);
}
/**
 * 验证查询实体必要的参是否具备
 * @author cao.weiwei
 * @date 2017年4月20日 下午5:14:13
 * @param tableDefineEntity
 * @return
 */
private boolean checkTableDefineEntity(TableDefineEntity tableDefineEntity){
	if(tableDefineEntity==null){
		return false;
	}else if(!StringUtils.isEmpty(tableDefineEntity.getId())||
			!StringUtils.isEmpty(tableDefineEntity.getDymcformId())||
			!StringUtils.isEmpty(tableDefineEntity.getTableName())){
		return true;
	}
	return false;
}
/**
* TableDefineEntity详细页 
* 
* */
@RequestMapping(value = "/EditPage")
public ModelAndView tableDefineEditPage(TableDefineEntity tableDefineEntity) {
	
	return new ModelAndView(returnPath("tableDefineAdd"));
}
/**
 * 查询TableDefineEntity
 * 
 * */
@RequestMapping(value = "/queryByID")
public @ResponseBody String findTableDefineData(HttpServletRequest request) {
	String id = request.getParameter("id");
	TableDefineEntity tableDefineEntity =  tableDefineBizService.queryById(new BigInteger(id));
	return JSON.toJSONString(tableDefineEntity); 
}

/**
 * 保存或更新TableDefine 
 * 
 * */
@RequestMapping(value = "/saveTableDefine")
public @ResponseBody String saveTableDefineData(@RequestBody TableDefineEntity tableDefineEntity) {
	ResponseEntity rs = null;
	if(tableDefineEntity!=null){
		tableDefineEntity.setCurrentUser(UserContext.getUser());
		if(!StringUtils.isEmpty(tableDefineEntity.getTableName())){
			rs = tableDefineBizService.updateTableDefine(tableDefineEntity);
		}else{
			rs = tableDefineBizService.addTableDefine(tableDefineEntity);
		}
		
	}
	return JSON.toJSONString(rs);
}

/**
 * 
 * @author cao.weiwei
 * @date 2017年4月20日 下午3:26:18
 * @param tableDefineEntity
 * @return
 */
@RequestMapping(value = "/generateTable")
public @ResponseBody String generateTable(@RequestBody TableDefineEntity tableDefineEntity) {
	ResponseEntity rs = null;
	if(tableDefineEntity!=null){
		tableDefineEntity.setCurrentUser(UserContext.getUser());
		rs = tableDefineBizService.generateTable(tableDefineEntity);
	}
	return JSON.toJSONString(rs);
}
/**
 * 保存或更新TableDefine 
 * 
 * */
@RequestMapping(value = "/distinctRow")
public @ResponseBody String distinctRow(@RequestBody List<SubTableEntity> SubTableEntity) {
	ResponseEntity rs = tableDefineBizService.distinctRow(SubTableEntity);
	return JSON.toJSONString(rs);
}
/**
 * 导出TableDefineEntity
 * @author shensu
 * @date 2015-7-09  下午2:13:28
 * @return
 */
@RequestMapping(value = "/exportTableDefineExcel")
public void exportTableDefineExcel(TableDefineEntity tableDefineEntity,HttpServletResponse response) {
		tableDefineBizService.exportTableDefine(tableDefineEntity, response);
}

/**
 * 店铺选择器页面
 * @author cao.weiwei
 * @date 2017年4月19日 下午3:46:47
 * @return
 */
@RequestMapping(value = "/shopSelect")
public ModelAndView shopSelect() {
	return new ModelAndView(returnPath("custompage/page/shopselectPage"));
}
/**
 * 设备选择器页面
 * @author cao.weiwei
 * @date 2017年4月24日 上午11:17:21
 * @return
 */
@RequestMapping(value = "/equipmentselectPage")
public ModelAndView findEqList() {
	return new ModelAndView(returnPath("custompage/page/equipmentselectPage"));
}

/**
 * 子表数据添加页面
 * @author cao.weiwei
 * @date 2017年4月24日 下午7:16:28
 * @return
 */
@RequestMapping(value = "/addSubTablePage")
public ModelAndView addSubTablePage() {
	return new ModelAndView(returnPath("custompage/page/addSubTablePage"));
}
/**
 * 设备清单新增页面
 * @author cao.weiwei
 * @date 2017年4月25日 下午5:20:07
 * @return
 */
@RequestMapping(value = "/addequipment_list")
public ModelAndView addequipment_list() {
	return new ModelAndView(returnPath("custompage/page/addequipment_list"));
}
/**
 * 设备详情新增页面
 * @author cao.weiwei
 * @date 2017年4月25日 下午5:22:32
 * @return
 */
@RequestMapping(value = "/addequipment_detail")
public ModelAndView addequipment_detail() {
	return new ModelAndView(returnPath("custompage/page/addequipment_detail"));
}
/**
 * 仓库盘点新增页面
 * @author cao.weiwei
 * @date 2017年4月25日 下午5:23:08
 * @return
 */
@RequestMapping(value = "/addstorage_list")
public ModelAndView addstorage_list() {
	return new ModelAndView(returnPath("custompage/page/addstorage_list"));
}

@RequestMapping(value = "/eqBarCodeListPage")
public ModelAndView eqBarCodeListPage(PurBarcodeEntity purBarcodeEntity) {
	return new ModelAndView(returnPath("custompage/page/eqBarCodeListPage")).addObject("obj",purBarcodeEntity);
}
/***
 * 报废调拨单新增设备页面
 * @author cao.weiwei
 * @date 2017年4月30日 下午3:36:27
 * @return
 */
@RequestMapping(value = "/addequipment_listScrapCall")
public ModelAndView addequipment_listScrapCall() {
	return new ModelAndView(returnPath("custompage/page/addequipment_listScrapCall"));
}
/**
 * 库存调拨单新增设备页面
 * @author cao.weiwei
 * @date 2017年4月30日 下午3:37:02
 * @return
 */
@RequestMapping(value = "/addequipment_listInventoryCall")
public ModelAndView addequipment_listInventoryCall() {
	return new ModelAndView(returnPath("custompage/page/addequipment_listInventoryCall"));
}
/**
 * 领用单、安装单、一般调拨单新增设备类型页面
 * @author cao.weiwei
 * @date 2017年4月30日 下午3:37:02
 * @return
 */
@RequestMapping(value = "/addequipment_list_applyForUsage")
public ModelAndView addequipment_list_applyForUsage() {
	return new ModelAndView(returnPath("custompage/page/addequipment_list_applyForUsage"));
}

/**
 * 采购设备列表页面
 * @author cao.weiwei
 * @date 2017年5月15日 下午2:29:35
 * @param business_no
 * @return
 */
@RequestMapping(value = "/PurchaseEquipment")
public ModelAndView PurchaseEquipment() {
	return new ModelAndView(returnPath("custompage/page/PurchaseEquipment"));
}
/**
 * 入库单添加设备页面
 * @author cao.weiwei
 * @date 2017年5月15日 下午2:30:59
 * @return
 */
@RequestMapping(value = "/inNoticeAddEquipment")
public ModelAndView inNoticeAddEquipment() {
	
	return new ModelAndView(returnPath("custompage/page/inNoticeAddEquipment"));
}


/**
 * 定义
 * 
 */
private final String MODULE_PATH = "/tabledefine/";

//返回路径
private String returnPath(String jspName) {
	return MODULE_PATH + jspName;
}
}
